﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using SHKF.B2C.JuShuiTan.Model;
using SHKF.B2C.JuShuiTan.Network;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SHKF.B2C.JuShuiTan.Service.Controllers
{
    [Route("[controller]")]
    [ApiController]
    public class BaseController : ControllerBase
    {
        protected readonly JSTHttpClient httpClient;
        protected readonly ILogger<BaseController> logger;

        public BaseController(IServiceProvider serviceProvider, ILogger<BaseController> logger)
        {
            IConfiguration configuration = (IConfiguration)serviceProvider.GetService(typeof(IConfiguration));
            this.httpClient = (JSTHttpClient)serviceProvider.GetService(typeof(JSTHttpClient)); 
            this.logger = logger;
        }

        protected void WriteException(string fun, Exception ex)
        {
            if (this.logger != null)
            {
                this.logger.LogError($"fun:{fun}>> ex:{ex.Message}");
            }

        }

        protected void WriteInformation(string fun, string msg)
        {
            if (this.logger != null)
            {
                this.logger.LogInformation($"fun:{fun}>> msg:{msg}");
            }
        }

        protected BaseDto GetCompanyID()
        {
            BaseDto dto = new BaseDto(); 
            if (base.HttpContext.Request.Headers.ContainsKey("CompanyID"))
            {
                dto.code = 0;
                dto.msg = base.HttpContext.Request.Headers["CompanyID"]; 
            }
            else
            {
                dto.code = -1;
                dto.msg = "沒有收到公司名,请在Header添加CompanyID";
            }
            return dto;
        }

    }
}
